//导入
import VueRouter from "vue-router"
import Vue from 'vue'
import store from "../vuex"
//注册
Vue.use(VueRouter);

//导入需要的组件
import App from "../App.vue";
import Login from "../components/Login.vue"
import UserCenter from "../components/UserCenter.vue"
import Home from '../components/Home.vue'
//每日一句
import dayWords  from "../components/dayWords.vue";
//用户管理系统
import userInfo from "../components/userInfo.vue"
/* 注册系统 */
import Register from "../components/Register.vue"
/* 文件上传功能 */
import upLoad from "../components/upLoad.vue"
/* 用户添加（管理者页面） */
import addUser from "../components/addUser.vue"
/* 文件列表功能 */
import List from "../components/List.vue"
/* 修改人员功能 */
import userEdit from "../components/userEdit.vue"
//文件管理功能
import fileCenter from '../components/fileCenter.vue'
//3-创建路由对象
var router = new VueRouter({
    mode: 'history',
    //routes属性：表示配置路由匹配规则
    routes: [
        //每个路由规则，都是一个对象，这个规则对象，身上，有两个必须的属性：
        //属性1 是 path， 表示监听 哪个路由链接地址；
        //属性2 是 component， 表示，如果 路由是前面匹配到的 path ，则展示 component 属性对应的那个组件
        // {
        //     //为/路径设定重定向
        //     path: "/", redirect: "/login"
        // },
        {
            path: "/",
            component: App
        },
        {
            path: "/login",
            component: Login
        },
        {
            path:'/home',
            component:Home,
            redirect:'/home/daywords',
            children:[
                {
                    path:'daywords',
                    component:dayWords
                },
                {
                    path: "usercenter",
                    component: UserCenter
                },
                {
                    path:'userInfo',
                    component:userInfo,
                },
                {
                    path:'userEdit',
                    component:userEdit,
                },
                {
                    path:'upload',
                    component:upLoad
                },
                {
                    path:'adduser',
                    component:addUser
                },
                { 
                    path: "list",
                    component:List
                },
                {
                    path:"filecenter",
                    component:fileCenter
                }
              
            ]
        },
        {
            path:'/register',
            component:Register
        }
    ]
});
//前置路由守卫
router.beforeEach((to, from, next) => {
    // `to` 和 `from` 都是路由对象
    // console.log(to.path + ":" + from.path);
    // 判断是否已登录
    const loginuser = store.state.user;
    // console.log(loginuser);
    //判断是否是“/”
    if (to.path == "/") {
        //首页
        if (loginuser.id) {
            router.replace("/home");
            next(false);
        } else {
            router.replace("/login");
            next(false);
        }
    }
    next();
})

export default router;